<?php

/**
 * 监理实施细则
 * @author YangL
 * */

namespace Admin\Controller;

use Common\Controller\WahahabaseController;

class SupervisionExecutiveRulesController extends WahahabaseController {

    protected $department_model;
    protected $consignee_model;

    public function _initialize() {
        parent::_initialize();
        $this->deal_model = D("Common/SupervisionExecutiveRules");
        $this->department_model = D("Common/Department");
        $this->consignee_model = D("Common/Consignee");
        $this->check = array('code');
    }

    public function index() {
        $where = array("d.is_deleted" => array('neq', 1));
        $code = I('request.code');
        if ($code) {
            $where['code'] = array('like', "%$code%");
        }
        $this->getDepartLevel();
        $this->indexPage($where);
    }

    public function add() {
        $Department = $this->department_model->where(array('is_deleted' => 0))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
        $this->display();
    }

    public function add_post() {
        if (IS_POST) {
            // 合并底层数据
            $data = array_merge($_POST, $this->saveData());
            if ($this->deal_model->create() !== false) {
                $result = $this->deal_model->add($data);
                if ($result !== false) {
                    $cons = array(
                        "belong_to" => '1',
                        "table_id" => $result
                    );
                    foreach ($data['carbon_copy_department'] as $key => $vo) {
                        $cons['consignee_type'] = '2';
                        $cons['department_id'] = $vo;
                        $this->consignee_model->add($cons);
                    };
                    $this->success("添加成功！", U("SupervisionExecutiveRules/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
    }

    public function edit() {
        $this->getInfo();
    }

    public function edit_post() {
        if (IS_POST) {
            $data = array_merge($_POST, $this->saveData(2));
            if ($this->deal_model->create() !== false) {
                $result = $this->deal_model->save($data);
                if ($result !== false) {
                    $this->consignee_model->where(array('belong_to' => 1, 'table_id' => I("post.id", 0)))->delete();
                    $cons = array(
                        "belong_to" => '1',
                        "table_id" => I("post.id", 0)
                    );
                    foreach ($data['carbon_copy_department'] as $key => $vo) {
                        $cons['consignee_type'] = '2';
                        $cons['department_id'] = $vo;
                        $this->consignee_model->add($cons);
                    };
                    $this->success("保存成功！", U("SupervisionExecutiveRules/index"));
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
    }

    public function view() {
        $this->getDepartLevel();
        $this->getInfo();
    }

    private function getInfo() {
        $id = I('get.id', 0, 'intval');
        $work = $this->deal_model->where(array("id" => $id))->find();
        $this->assign("result", $work);

        $Department = $this->department_model->field("id,department_name")->where(array("is_deleted" => array('neq', 1)))->select();
        $this->assign("data", $Department);

        $Department = $this->consignee_model->field("department_id")->where(array('consignee_type' => 1, 'belong_to' => 1, 'table_id' => $id))->select();
        $result = array();
        foreach ($Department as $key => $vo) {
            $result[] = $vo['department_id'];
        }
        $this->assign("dept1", json_encode($result));

        $Department = $this->consignee_model->field("department_id")->where(array('consignee_type' => 2, 'belong_to' => 1, 'table_id' => $id))->select();
        $result = array();
        foreach ($Department as $key => $vo) {
            $result[] = $vo['department_id'];
        }
        $this->assign("dept2", json_encode($result));

        $this->display();
    }

}

?>